package top.yokey.shopnc.activity.order;

import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatRadioButton;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;

import top.yokey.shopnc.R;
import top.yokey.shopnc.base.BaseApplication;
import top.yokey.shopnc.base.BaseConstant;
import top.yokey.base.base.BaseCountTime;
import top.yokey.base.base.BaseHttpListener;
import top.yokey.base.base.BaseSnackBar;
import top.yokey.base.base.BaseToast;
import top.yokey.shopnc.base.SwipeActivity;
import top.yokey.base.bean.BaseBean;
import top.yokey.shopnc.model.MemberBuyModel;
import top.yokey.shopnc.model.MemberPaymentModel;
import top.yokey.base.util.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_order_pay)
public class PayActivity extends SwipeActivity {

    @ViewInject(R.id.mainLinearLayout)
    private LinearLayoutCompat mainLinearLayout;
    @ViewInject(R.id.mainToolbar)
    private Toolbar mainToolbar;

    @ViewInject(R.id.moneyTextView)
    private AppCompatTextView moneyTextView;
    @ViewInject(R.id.snTextView)
    private AppCompatTextView snTextView;

    @ViewInject(R.id.ownLinearLayout)
    private LinearLayoutCompat ownLinearLayout;
    @ViewInject(R.id.preDepositRelativeLayout)
    private RelativeLayout preDepositRelativeLayout;
    @ViewInject(R.id.preDepositTextView)
    private AppCompatTextView preDepositTextView;
    @ViewInject(R.id.preDepositRadioButton)
    private AppCompatRadioButton preDepositRadioButton;
    @ViewInject(R.id.rechargeCardRelativeLayout)
    private RelativeLayout rechargeCardRelativeLayout;
    @ViewInject(R.id.rechargeCardTextView)
    private AppCompatTextView rechargeCardTextView;
    @ViewInject(R.id.rechargeCardRadioButton)
    private AppCompatRadioButton rechargeCardRadioButton;
    @ViewInject(R.id.passwordRelativeLayout)
    private RelativeLayout passwordRelativeLayout;
    @ViewInject(R.id.passwordEditText)
    private AppCompatEditText passwordEditText;

    @ViewInject(R.id.thrLinearLayout)
    private LinearLayoutCompat thrLinearLayout;
    @ViewInject(R.id.aliPayRelativeLayout)
    private RelativeLayout aliPayRelativeLayout;
    @ViewInject(R.id.aliPayRadioButton)
    private AppCompatRadioButton aliPayRadioButton;
    @ViewInject(R.id.wxPayRelativeLayout)
    private RelativeLayout wxPayRelativeLayout;
    @ViewInject(R.id.wxPayRadioButton)
    private AppCompatRadioButton wxPayRadioButton;

    @ViewInject(R.id.payTextView)
    private AppCompatTextView payTextView;

    private String paySnString;
    private String passwordString;
    private String rcbPayString;
    private String pdPayString;
    private String paymentCodeString;

    @Override
    public void initData() {

        paySnString = getIntent().getStringExtra(BaseConstant.DATA_ID);
        if (TextUtils.isEmpty(paySnString)) {
            BaseToast.get().showDataError();
            BaseApplication.get().finish(getActivity());
        }

        setToolbar(mainToolbar, "订单支付");

        snTextView.setText("订单：");
        snTextView.append(paySnString);

        passwordString = "";
        rcbPayString = "";
        pdPayString = "";
        paymentCodeString = "";

        getData();

    }

    @Override
    public void initEven() {

        preDepositRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                preDepositRadioButton.setChecked(true);
            }
        });

        preDepositRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    rechargeCardRadioButton.setChecked(false);
                    aliPayRadioButton.setChecked(false);
                    wxPayRadioButton.setChecked(false);
                    passwordRelativeLayout.setVisibility(View.VISIBLE);
                }
            }
        });

        rechargeCardRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rechargeCardRadioButton.setChecked(true);
            }
        });

        rechargeCardRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    preDepositRadioButton.setChecked(false);
                    aliPayRadioButton.setChecked(false);
                    wxPayRadioButton.setChecked(false);
                    passwordRelativeLayout.setVisibility(View.VISIBLE);
                }
            }
        });

        aliPayRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                aliPayRadioButton.setChecked(true);
            }
        });

        aliPayRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    preDepositRadioButton.setChecked(false);
                    rechargeCardRadioButton.setChecked(false);
                    wxPayRadioButton.setChecked(false);
                    passwordRelativeLayout.setVisibility(View.GONE);
                }
            }
        });

        wxPayRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                wxPayRadioButton.setChecked(true);
            }
        });

        wxPayRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    preDepositRadioButton.setChecked(false);
                    rechargeCardRadioButton.setChecked(false);
                    aliPayRadioButton.setChecked(false);
                    passwordRelativeLayout.setVisibility(View.GONE);
                }
            }
        });

        payTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean check = false;
                if (preDepositRadioButton.isChecked()) {
                    check = true;
                }
                if (rechargeCardRadioButton.isChecked()) {
                    check = true;
                }
                if (aliPayRadioButton.isChecked()) {
                    check = true;
                }
                if (wxPayRadioButton.isChecked()) {
                    check = true;
                }
                if (!check) {
                    BaseSnackBar.get().show(mainLinearLayout, "请选择支付方式！");
                    return;
                }
                pay();
            }
        });

    }

    //自定义方法

    private void pay() {

        pdPayString = preDepositRadioButton.isChecked() ? "1" : "0";
        rcbPayString = rechargeCardRadioButton.isChecked() ? "1" : "0";

        if (pdPayString.equals("1") || rcbPayString.equals("1")) {

            passwordString = passwordEditText.getText().toString();
            if (TextUtils.isEmpty(passwordString)) {
                BaseSnackBar.get().show(mainLinearLayout, "请输入支付密码！");
                return;
            }

            payTextView.setEnabled(false);
            payTextView.setText("支付中...");

            MemberBuyModel.get().checkPdPwd(passwordString, new BaseHttpListener() {
                @Override
                public void onSuccess(BaseBean baseBean) {
                    if (JsonUtil.isSuccess(baseBean.getDatas())) {
                        ownPay();
                    } else {
                        payTextView.setEnabled(true);
                        payTextView.setText("订单支付");
                        BaseSnackBar.get().showFailure(mainLinearLayout);
                    }
                }

                @Override
                public void onFailure(String reason) {
                    payTextView.setEnabled(true);
                    payTextView.setText("订单支付");
                    BaseSnackBar.get().show(mainLinearLayout, reason);
                }
            });

        }

    }

    private void ownPay() {

        MemberPaymentModel.get().payNew(paySnString, passwordString, rcbPayString, pdPayString, new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                payTextView.setEnabled(true);
                payTextView.setText("订单支付");
                BaseToast.get().show("支付成功");
                BaseApplication.get().finish(getActivity());
            }

            @Override
            public void onFailure(String reason) {
                payTextView.setEnabled(true);
                payTextView.setText("订单支付");
                BaseToast.get().show("支付成功");
                BaseApplication.get().finish(getActivity());
            }
        });

    }

    private void getData() {

        BaseSnackBar.get().showHandler(mainLinearLayout);

        MemberBuyModel.get().pay(paySnString, new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                try {
                    String data = JsonUtil.getDatasString(baseBean.getDatas(), "pay_info");
                    JSONObject jsonObject = new JSONObject(data);
                    //站内支付
                    double payAmount = Double.parseDouble(jsonObject.getString("pay_amount"));
                    double preDeposit = Double.parseDouble(jsonObject.getString("member_available_pd"));
                    double rechargeCard = Double.parseDouble(jsonObject.getString("member_available_rcb"));
                    if (preDeposit > payAmount) {
                        ownLinearLayout.setVisibility(View.VISIBLE);
                        preDepositRelativeLayout.setVisibility(View.VISIBLE);
                        preDepositTextView.append("（可用金额：￥" + jsonObject.getString("member_available_pd") + "）");
                    }
                    if (rechargeCard > payAmount) {
                        ownLinearLayout.setVisibility(View.VISIBLE);
                        rechargeCardRelativeLayout.setVisibility(View.VISIBLE);
                        rechargeCardTextView.append("（可用金额：￥" + jsonObject.getString("member_available_rcb") + "）");
                    }
                    //第三方支付
                    if (data.contains("wxpay")) {
                        thrLinearLayout.setVisibility(View.VISIBLE);
                        wxPayRelativeLayout.setVisibility(View.VISIBLE);
                    }
                    if (data.contains("alipay")) {
                        thrLinearLayout.setVisibility(View.VISIBLE);
                        aliPayRelativeLayout.setVisibility(View.VISIBLE);
                    }
                    moneyTextView.setText("￥");
                    moneyTextView.append(payAmount + "");
                    payTextView.setText("订单支付：￥");
                    payTextView.append(payAmount + "");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String reason) {
                BaseSnackBar.get().show(mainLinearLayout, reason);
                new BaseCountTime(BaseConstant.TIME_COUNT, BaseConstant.TIME_TICK) {
                    @Override
                    public void onFinish() {
                        super.onFinish();
                        getData();
                    }
                }.start();
            }
        });

    }

}
